package tau.dimes.ipGeo;

import java.util.Map;
import java.util.TreeMap;

import tau.dimes.config.ipGeo.IpGeoConfiguration;
import tau.dimes.config.ipGeo.IpGeoProvider;

public class IpGeoFactory
{
	private static final Map<String, IpGeoProviderImpl> providers;

	static
	{
		providers = new TreeMap<String, IpGeoProviderImpl>();
	}

	public static IpGeoProviderImpl create() throws IpGeoException
	{
		String providerName = IpGeoConfiguration.getDefaultProviderName();
		return create(providerName);
	}

	public static IpGeoProviderImpl create(String name) throws IpGeoException
	{
		IpGeoProviderImpl provider = providers.get(name);

		if (provider == null)
		{
			IpGeoProvider prov = IpGeoConfiguration.getProvider(name);
			if (prov == null)
				throw new IpGeoException("Can't find provider: " + name);
			provider = new IpGeoProviderImpl(prov);
			providers.put(name, provider);
		}

		return provider;
	}
}
